package com.anjho.bean;

import java.io.Serializable;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;

import com.anjho.dao.DAOPublisher;
import com.anjho.exceptions.InvalidGenreException;
import com.anjho.exceptions.InvalidPublisherException;
import com.anjho.pojo.Publisher;

@ManagedBean(name = "publisherBean")
@RequestScoped
public class PublisherBean extends GenericBean implements Serializable {

	private static final long serialVersionUID = -3134517238480190041L;
	
	private List<Publisher> publisherList;
	private Publisher publisher;
	private String description;
	private DAOPublisher daoPublisher;

	public PublisherBean() {
		super();
		this.setPublisher(new Publisher());
		this.setPublisherList(new ArrayList<Publisher>());
		daoPublisher = new DAOPublisher();
	}

	public void setPublisherList(List<Publisher> publisherList) {
		this.publisherList = publisherList;
	}

	public List<Publisher> getPublisherList() throws Exception {
		synchronized (this) {
			publisherList = daoPublisher.getPublisherList();
			return publisherList;
		}
	}

	public Publisher getPublisher() {
		return publisher;
	}

	public void setPublisher(Publisher publisher) {
		this.publisher = publisher;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public void onDeleteClick() {
		try {
			if ((this.publisher == null) || (this.publisher.getDescription().equals(""))){
				throw new InvalidGenreException("É necessário selecionar uma editora antes de executar esta operação!");
			}
			daoPublisher.delete(publisher);
		} catch (SQLException ex) {
			FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_FATAL, "Erro na base de dados", ex.getMessage());
			FacesContext.getCurrentInstance().addMessage(null, facesMsg);
		} catch (InvalidPublisherException ex) {
			FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_FATAL, "Editora inválida", ex.getMessage());
			FacesContext.getCurrentInstance().addMessage(null, facesMsg);
		} catch (Exception ex) {
			FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_FATAL, "Erro desconhecido", "Houve um erro não identificado.");
			FacesContext.getCurrentInstance().addMessage(null, facesMsg);
		}
	}

	public void onSaveClick() {
		try {
			daoPublisher.update(this.publisher);
		} catch (Exception e) {
			FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_FATAL, null, "Houve um erro não identificado.");
			FacesContext.getCurrentInstance().addMessage(null, facesMsg);
		}
	}

	public void onRegisterClick() {
		try {
			Publisher aux = new Publisher();
			aux.setDescription(this.description);
			daoPublisher.insert(aux);
			this.description = "";
		} catch (Exception ex) {
			FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_FATAL, null, "Houve um erro não identificado.");
			FacesContext.getCurrentInstance().addMessage(null, facesMsg);
		}
	}
}
